package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"gorm.io/gorm"
)

type EmailTemplateService struct {
}

// CreateEmailTemplate 创建短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) CreateEmailTemplate(emailTemplate *business.EmailTemplate) (err error) {
	err = global.GVA_DB.Create(emailTemplate).Error
	return err
}

// DeleteEmailTemplate 删除短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) DeleteEmailTemplate(emailTemplate business.EmailTemplate) (err error) {
	db := global.GVA_DB.Model(&business.EmailTemplate{}).Where("id = ?", emailTemplate.ID)
	if emailTemplate.UserId != 0 {
		db = db.Where("user_id = ? ", emailTemplate.UserId)
	}
	err = db.Delete(&emailTemplate).Error
	return err
}

// DeleteEmailTemplateByIds 批量删除短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) DeleteEmailTemplateByIds(ids request.IdsReq, userId uint) (err error) {
	db := global.GVA_DB.Model(&business.EmailTemplate{}).Where("id in (?)", ids)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = global.GVA_DB.Delete(&business.EmailTemplate{}).Error
	return err
}

// UpdateEmailTemplate 更新短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) UpdateEmailTemplate(emailTemplate business.EmailTemplate) (err error) {
	emailTemplateMap := map[string]interface{}{
		"name": emailTemplate.Name,
		"msg":  emailTemplate.Msg,
	}
	err = global.GVA_DB.Model(&business.EmailTemplate{}).Where("id =?", emailTemplate.ID).Updates(emailTemplateMap).Error
	return err
}

// GetEmailTemplate 根据id获取短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) GetEmailTemplate(id uint, userId uint) (emailTemplate business.EmailTemplate, err error) {
	db := global.GVA_DB.Where("id = ? ", id)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = db.First(&emailTemplate).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetEmailTemplateInfoList 分页获取短息模板记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailTemplateService *EmailTemplateService) GetEmailTemplateInfoList(info businessReq.EmailTemplateSearch) (list []business.EmailTemplate, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&business.EmailTemplate{})
	var emailTemplates []business.EmailTemplate
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&emailTemplates).Error
	return emailTemplates, total, err
}
